package com.bah.tract.client.workstore;

import java.util.HashMap;
import java.util.Map;
import java.util.prefs.Preferences;

import javax.inject.Inject;
import javax.inject.Singleton;

import com.bah.tract.client.workstore.jira.JiraTaskProvider;
import com.bah.tract.client.workstore.project.ProjectTaskProvider;
import com.bah.tract.client.workstore.tol.TOLTaskProvider;
import com.bah.tract.mapping.PropertyHelper;
import com.bah.tract.security.PasswordEncryptor;
import com.bah.tract.service.WorkStore;

/**
 * @author 524506
 */
@Singleton
public final class TaskProviderFactory {

	/**
	 * @uml.property name="mTolProvider"
	 * @uml.associationEnd
	 */
	private final TOLTaskProvider mTolProvider;
	private final Preferences mPreferences;
	private final PasswordEncryptor mPasswordEncryptor;

	@Inject
	TaskProviderFactory(final TOLTaskProvider tolProvider,
			final PasswordEncryptor passwordEncryptor,
			final Preferences preferences) {
		mTolProvider = tolProvider;
		mPasswordEncryptor = passwordEncryptor;
		mPreferences = preferences;
	}

	public TaskProvider getTaskProvider(final WorkStore workStore) {
		// Put workStore Properties into a map for easier access.
		final Map<String, Object> properties = new HashMap<String, Object>();
		PropertyHelper.processProperties(properties, workStore.getProperty());
		properties.put("label", workStore.getLabel());
		if ("TOLStore".equals(workStore.getType())) {
			return mTolProvider;
		} else if ("JiraStore".equals(workStore.getType())) {
			return new JiraTaskProvider(mPasswordEncryptor, mPreferences,
					properties, workStore.getId());
		} else if ("MSProjectStore".equals(workStore.getType())) {
			return new ProjectTaskProvider(properties, workStore.getId());
		} else {
			throw new IllegalArgumentException();
		}
	}
}
